import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.lightGreen,
      ),
      //关掉右上角debug标志
      debugShowCheckedModeBanner: false,
      home: MyHomePage(title: '打折计算器'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  //输入框
  TextEditingController _textEditingController =
      new TextEditingController.fromValue(TextEditingValue(text: "15"));

  Widget buildTextField(TextEditingController _textEditingController) {
    return TextField(
      //控制正在编辑的文本。通过其可以拿到输入的文本值
      //获取方式 String value=controller.text
      controller: _textEditingController,

      //给TextField设置装饰（形状等）
      decoration: InputDecoration(
        // border: OutlineInputBorder(
        //     borderRadius: BorderRadius.all(Radius.circular(30)),
        //     borderSide: BorderSide(color: Colors.transparent)),
        // //输入内容距离上下左右的距离 ，可通过这个属性来控制 TextField的高度
        contentPadding: EdgeInsets.all(10.0),
        fillColor: Colors.white, filled: true,
        labelText: '价格',
        labelStyle: TextStyle(
          color: Colors.red,
          fontSize: 20,
        ),
        helperText: "输入原价",
        icon: Icon(Icons.person),
        // 以下属性可用来去除TextField的边框
        // disabledBorder: InputBorder.none,
        // enabledBorder: InputBorder.none,
        // focusedBorder: InputBorder.none,
      ),

      //键盘类型
      keyboardType: TextInputType.multiline,

      //控制键盘的功能键 指enter键，比如此处设置为next时，enter键
      //显示的文字内容为 下一步
      textInputAction: TextInputAction.next,

      //键盘大小写的显示 Only supports text keyboards  但是好像不起作用？
      //characters 默认为每个字符使用大写键盘
      //sentence 默认为每个句子的第一个字母使用大写键盘
      //word 默认为每个单词的第一个字母使用大写键盘。
      //none 默认使用小写
      textCapitalization: TextCapitalization.words,

      //是否是密码
      obscureText: false,

      //文本对齐方式(即光标初始位置)
      textAlign: TextAlign.center,

      //输入文本的样式
      style: TextStyle(fontSize: 30.0, color: Colors.blue),

      //最大行数
      maxLines: 1,

      //最大长度，设置此项会让TextField右下角有一个输入数量的统计字符串
      //这种情况一般是不符合我们设计的要求的，但是有需要限制其输入的位数
      // 这时候我们可以使用下方的属性来限制
      maxLength: 9,

      // 跟最大长度限制对应，如果此属性设置为false,当输入超过最大长度
      // 限制时，依然会显示输入的内容，为true不会（默认为 true）
      maxLengthEnforced: false,

      // 限制输入的 最大长度  TextField右下角没有输入数量的统计字符串
//      inputFormatters: [LengthLimitingTextInputFormatter(11)],

      //允许的输入格式 下方的模式指只允许输入数字
      inputFormatters: [WhitelistingTextInputFormatter.digitsOnly],

      //控制此小部件是否具有键盘焦点。
      focusNode: FocusNode(),

      //是否自动更正
      autocorrect: false,

      //是否自动获得焦点
      autofocus: false,

      //是否禁用textfield:如果为false, textfield将被“禁用”
      enabled: true,

      //光标颜色
      cursorColor: Colors.red,

      //光标宽度
      cursorWidth: 5.0,

      //光标圆角弧度
      cursorRadius: Radius.circular(5.0),

      //内容改变的回调
      onChanged: (text) {
        print('change $text');
      },

      //内容提交(按回车)的回调
      onSubmitted: (text) {
        print('submit $text');
      },

      //按回车时调用
      onEditingComplete: () {
        print('onEditingComplete');
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
        appBar: AppBar(
          // Here we take the value from the MyHomePage object that was created by
          // the App.build method, and use it to set our appbar title.
          title: Text(widget.title),
        ),
        body: Center(
            // Center is a layout widget. It takes a single child and positions it
            // in the middle of the parent.
            child: Column(
                // Column is also layout widget. It takes a list of children and
                // arranges them vertically. By default, it sizes itself to fit its
                // children horizontally, and tries to be as tall as its parent.
                //
                // Invoke "debug painting" (press "p" in the console, choose the
                // "Toggle Debug Paint" action from the Flutter Inspector in Android
                // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
                // to see the wireframe for each widget.
                //
                // Column has various properties to control how it sizes itself and
                // how it positions its children. Here we use mainAxisAlignment to
                // center the children vertically; the main axis here is the vertical
                // axis because Columns are vertical (the cross axis would be
                // horizontal).
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
              new TextField(
                controller: _textEditingController,
              ),
              new RaisedButton(
                onPressed: () {
                  print(_textEditingController.text);
                  _textEditingController.clear();
                },
                child: Text('清除'),
              ),
              Table(children: _tableRowList().toList())
            ])));
  }

  List<TableRow> _tableRowList() {
    //1.0---0.1   20
    dynamic content;
    List<TableRow> Tlist = <TableRow>[
      TableRow(
        children: [
          Center(child: Text('折扣（百分制）')),
          Center(child: Text('价格（元）')),
          Center(child: Text('亏损（元）')),
        ],
      ),
    ];
    for (var i = 100; i >= 1;) {
      content = TableRow(
        children: [
          Center(child: Text(i.toString() + "折")),
          Center(
              child: Text(
                  (_textEditingController.toString() * i).toString() + "元")),
          Center(child: Text(i.toString() + "元"))
        ],
      );
      Tlist.add(content);
      i = i - 5;
    }
    return Tlist;
  }
}
